package game.zqll.systems.input;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import game.zqll.components.*;
import game.zqll.systems.Mappers;
import game.zqll.systems.PlayerSystem;
import net.mostlyoriginal.api.system.core.PassiveSystem;

import static game.zqll.systems.input.KeyBindings.Key.MOVE_LEFT;
import static game.zqll.systems.input.KeyBindings.Key.MOVE_RIGHT;

public class InputSystem extends PassiveSystem implements InputProcessor {

    private PlayerSystem playerSystem;
    public static final float SPEED = 200f;

    @Override
    protected void initialize() {
        InputProcessor inputProcessor = Gdx.input.getInputProcessor();
        if (inputProcessor == null) {
            Gdx.input.setInputProcessor(this);
        }
    }

    @Override
    public boolean keyDown(int keycode) {
        KeyBindings.Key key = KeyBindings.key(keycode);
        MoveComp moveComp = Mappers.move.get(playerSystem.get());
        switch (key) {
            case MOVE_LEFT -> moveComp.velocity.x = -SPEED;
            case MOVE_RIGHT -> moveComp.velocity.x = SPEED;
        }
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        KeyBindings.Key key = KeyBindings.key(keycode);
        MoveComp moveComp = Mappers.move.get(playerSystem.get());
        switch (key) {
            case MOVE_LEFT -> moveComp.velocity.x = KeyBindings.isKeyPressed(MOVE_RIGHT) ? SPEED : 0;
            case MOVE_RIGHT -> moveComp.velocity.x = KeyBindings.isKeyPressed(MOVE_LEFT) ? -SPEED : 0;
        }
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchCancelled(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(float amountX, float amountY) {
        return false;
    }
}
